package name.bingo.bing.pay.service.message.queue.controller;
/**
 * create by yanbing on 2018/11/12 10:54
 */

import name.bingo.bing.pay.service.message.queue.config.ActiveMQ4Config;
import name.bingo.bing.pay.service.message.queue.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @param:
 * @return:
 * @auther: yanbing
 * @date: 2018/11/12 10:54
 **/
@Component
public class BankMessageListener {
    @Autowired
    private MessageService messageService;
    //queue
    private static final String BankMessage=ActiveMQ4Config.BankMessage;
    private static final String AccountMessage=ActiveMQ4Config.AccountMessage;

    @JmsListener(destination = BankMessage, containerFactory = "jmsQueueListener")
    public void receiveQueue(final TextMessage text, Session session)throws JMSException {
        try {
            text.getJMSMessageID();
            System.out.println("收到支付回调消息:" + text.getText());
            messageService.handerBankMessage(text.getText());
            text.acknowledge();// 使用手动签收模式，需要手动的调用，如果不在catch中调用session.recover()消息只会在重启服务后重发
        } catch (Exception e) {
            System.out.println("mq receive:"+e.getMessage());
            //session.recover();// 此不可省略 重发信息使用
        }
    }
    @JmsListener(destination = AccountMessage, containerFactory = "jmsQueueListener")
    public void accountQueue(final TextMessage text, Session session)throws JMSException {
        try {
            text.getJMSMessageID();
            System.out.println("收到会计消息:" + text.getText());
            messageService.handerAccountMessage(text.getText());
            text.acknowledge();// 使用手动签收模式，需要手动的调用，如果不在catch中调用session.recover()消息只会在重启服务后重发
        } catch (Exception e) {
            System.out.println("mq receive:"+e.getMessage());
            session.recover();// 此不可省略 重发信息使用
        }
    }
    @JmsListener(destination="study.topic", containerFactory="jmsTopicListener")// 增加对应处理的监听器工程
    public void receiveTopic(TextMessage text) throws JMSException{
        System.out.println(text.getText());
    }


}
